"""
Case Type   : gs_checkse工具功能
Case Name   : 验证gs_checkse工具输出日志功能
Create At   : 2024/10/21
Owner       : lihongji
Description :
    1.gs_checkse安全检测日志输出到指定目录
    2.gs_checkse安全检测日志输出到默认目录
    3.gs_checkse安全修复日志输出到指定目录
    4.gs_checkse安全修复日志输出到默认目录
    5.gs_checkse安全检测工具不指定--detail参数
    6.gs_checkse安全修复工具不指定--detail参数
Expect      :
    1.成功
    2.成功
    3.成功
    4.成功
    5.不打印检测详情
    6.不打印修复详情
History     :
"""
import os
import unittest

from testcase.utils.Logger import Logger
from testcase.utils.Common import Common
from yat.test import Node
from yat.test import macro


class GS_CHECKSE(unittest.TestCase):
    def setUp(self):
        self.log = Logger()
        self.log.info(f'----{os.path.basename(__file__)} start----')
        self.primaryNode = Node('PrimaryDbUser')
        self.common = Common()
        self.env_path = macro.DB_ENV_PATH
        self.A_log_txt = 'gs_checkse_A.log'
        self.B_log_txt = 'gs_checkse_B.log'
        self.A_log_path = os.path.join(macro.DB_BACKUP_PATH, self.A_log_txt)
        self.B_log_path = os.path.join(macro.DB_BACKUP_PATH, self.B_log_txt)
        self.gaussdb_log = os.path.dirname(macro.PG_LOG_PATH)
        self.default_log_path = os.path.join(self.gaussdb_log,
                                             self.primaryNode.ssh_user, 'om')
        self.log.info("验证日志"+self.default_log_path)

    def test_gscheckse(self):

        step = '----step1:gs_checkse安全检测日志输出到指定目录 expect:成功----'
        self.log.info(step)
        gs_checkse_cmd = (f'source {self.env_path};'
                          f'gs_checkse -i A --detail -l {self.A_log_path}')
        check_file_cmd = (f"source {self.env_path};"
                          f"ls -l {macro.DB_BACKUP_PATH}")
        self.log.info(gs_checkse_cmd)
        check_res = self.common.get_sh_result(self.primaryNode, gs_checkse_cmd)
        check_file_res = self.common.get_sh_result(self.primaryNode,
                                                   check_file_cmd)
        self.assertIn("A1. [ Checking Connection configuration ]",
                      check_res, '执行失败' + step)
        self.assertIn("gs_checkse_A", check_file_res, '执行失败' + step)

        step = '----step2:gs_checkse安全检测日志输出到默认目录 expect:成功----'
        self.log.info(step)
        gs_checkse_cmd = (f'source {self.env_path};'
                          f'gs_checkse -i A --detail')
        check_default_file = (f"source {self.env_path};"
                              f"ls -l {self.default_log_path}")
        self.log.info(gs_checkse_cmd)
        self.log.info(check_default_file)
        check_res = self.common.get_sh_result(self.primaryNode, gs_checkse_cmd)
        check_defaut_file_res = self.common.get_sh_result(self.primaryNode,
                                                          check_default_file)
        self.assertIn("A1. [ Checking Connection configuration ]",
                      check_res, '执行失败' + step)
        self.assertIn("Checking File directory security",
                      check_res, '执行失败' + step)
        self.assertIn("gs_checkse", check_defaut_file_res,
                      '执行失败' + step)

        step = '----step3:gs_checkse安全修复日志输出到指定目录 expect:成功----'
        self.log.info(step)
        gs_checkse_cmd = (f'source {self.env_path};'
                          f'gs_checkse -i B --detail -l {self.B_log_path}')
        check_file_cmd = (f"source {self.env_path};"
                          f"ls -l {macro.DB_BACKUP_PATH}")
        self.log.info(gs_checkse_cmd)
        check_res = self.common.get_sh_result(self.primaryNode,
                                                   gs_checkse_cmd)
        check_file_res = self.common.get_sh_result(self.primaryNode,
                                                   check_file_cmd)
        self.assertIn("gs_checkse_B", check_file_res, '执行失败' + step)
        self.assertIn("Set Connection configuration", check_res,
                      '执行失败' + step)

        step = '----step4:gs_checkse安全修复日志输出到默认目录 expect:成功----'
        self.log.info(step)
        gs_checkse_cmd = (f'source {self.env_path};'
                          f'gs_checkse -i B --detail')
        check_default_file = (f"source {self.env_path};"
                              f"ls -l {self.default_log_path}")
        self.log.info(gs_checkse_cmd)
        self.log.info(check_default_file)
        check_res = self.common.get_sh_result(self.primaryNode, gs_checkse_cmd)
        check_defaut_file_res = self.common.get_sh_result(self.primaryNode,
                                                          check_default_file)
        self.assertIn("B1. [ Set Connection configuration ]",
                      check_res, '执行失败' + step)
        self.assertIn("Setting Connection configuration succeed",
                      check_res, '执行失败' + step)
        self.assertIn("gs_checkse", check_defaut_file_res,
                      '执行失败' + step)

        step = ('----step5:gs_checkse安全检测工具不指定--detail参数 '
                'expect:不打印检测详情----')
        self.log.info(step)
        gs_checkse_cmd = (f'source {self.env_path};'
                          f'gs_checkse -i A')
        self.log.info(gs_checkse_cmd)
        check_res = self.common.get_sh_result(self.primaryNode, gs_checkse_cmd)
        self.assertNotIn("Checking File directory security succeeded.",
                         check_res, '执行失败' + step)

        step = ('----step6:gs_checkse安全修复工具不指定--detail参数 '
                'expect:不打印检测详情----')
        self.log.info(step)
        gs_checkse_cmd = (f'source {self.env_path};'
                          f'gs_checkse -i B')
        self.log.info(gs_checkse_cmd)
        check_res = self.common.get_sh_result(self.primaryNode, gs_checkse_cmd)
        self.assertNotIn("Setting Connection configuration succeed.",
                         check_res, '执行失败' + step)

    def tearDown(self):
        step = '----step5:清理生成的安全检测报告与修复报告 expect:成功----'
        self.log.info(step)
        del_file = f"rm -rf {self.A_log_path} {self.B_log_path}"
        self.log.info(del_file)
        del_res = self.common.get_sh_result(self.primaryNode, del_file)
        self.assertEqual('', del_res, '执行失败：' + step)
        self.log.info(f'----{os.path.basename(__file__)} end----')
